package com.smartfast4j.log4j.ext;

import com.mongodb.DBObject;
import org.log4mongo.MongoDbAppender;

/**
 * 扩展MongoDbAppender，添加自定义字段
 */
public class MongoDbExtAppender extends MongoDbAppender {

    @Override
    public void append(final DBObject bson) {
        try {
            String logLevel = (String) bson.get("level");
            if(logLevel.equals("ERROR")){
                //获取错误的堆栈信息
                Object throwables = bson.get("throwables");
                bson.put("message",throwables);
            }

            DBObject host = (DBObject) bson.get("host");
            bson.put("ip",host.get("ip"));
            bson.put("serverIp",IpUtil.getLocalIP());
            bson.put("author","孤傲苍狼");
            super.append(bson);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
